package com.core.gui.dialog;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

import com.core.dao.DAOException;
import com.core.dao.DAOFactory;
import com.core.dao.impl.ContactDAOImpl;
import com.core.dao.impl.DBUtil;
import com.core.dao.impl.UsernameDAOImpl;
import com.core.entities.Contact;
import com.core.entities.Username;
import com.core.gui.helper.UserInfo;
import com.core.service.UsernamSrv;

public class RegistrationDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private final JLabel lblFirstName = new JLabel("First Name *:");
	private final JTextField firstNameTextField = new JTextField();
	private final JLabel lblSecondName = new JLabel("Second Name :");
	private final JTextField secondNameextField = new JTextField();
	private final JLabel lblLastName = new JLabel("Last Name *:");
	private final JTextField lastNameTextField = new JTextField();
	private final JLabel lblMobileNumber = new JLabel("Mobile Number :");
	private final JTextField MobilPhoneTextField = new JTextField();
	private final JLabel lblEmail = new JLabel("E-Mail :");
	private final JTextField emailTextField = new JTextField();
	private final JSeparator separator = new JSeparator();
	private final JLabel lblUsername = new JLabel("Username *:");
	private final JTextField usernameTextField = new JTextField();
	private final JLabel lblPassword = new JLabel("Password *:");
	private final JPasswordField passwordField = new JPasswordField();
	private final JLabel lblRepassword = new JLabel("Re-Password *:");
	private final JPasswordField repasswordField = new JPasswordField();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {

		try {

			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (UnsupportedLookAndFeelException e) {
			// handle exception
		} catch (ClassNotFoundException e) {
			// handle exception
		} catch (InstantiationException e) {
			// handle exception
		} catch (IllegalAccessException e) {
			// handle exception
		}
		try {
			RegistrationDialog dialog = new RegistrationDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */

	public RegistrationDialog() {

		usernameTextField.setBounds(75, 126, 170, 20);
		emailTextField.setBounds(50, 88, 271, 20);
		emailTextField.setColumns(10);
		MobilPhoneTextField.setBounds(90, 63, 148, 20);
		MobilPhoneTextField.setColumns(10);
		lastNameTextField.setBounds(400, 38, 86, 20);
		lastNameTextField.setColumns(10);
		secondNameextField.setBounds(240, 38, 86, 20);
		secondNameextField.setColumns(10);
		firstNameTextField.setBounds(75, 38, 86, 20);
		firstNameTextField.setColumns(10);
		setBounds(100, 100, 526, 278);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		lblFirstName.setBounds(10, 40, 65, 14);

		contentPanel.add(lblFirstName);

		contentPanel.add(firstNameTextField);
		lblSecondName.setBounds(166, 41, 72, 14);

		contentPanel.add(lblSecondName);

		contentPanel.add(secondNameextField);
		lblLastName.setBounds(336, 41, 63, 14);

		contentPanel.add(lblLastName);

		contentPanel.add(lastNameTextField);
		lblMobileNumber.setBounds(10, 66, 77, 14);

		contentPanel.add(lblMobileNumber);

		contentPanel.add(MobilPhoneTextField);
		lblEmail.setBounds(10, 91, 35, 14);

		contentPanel.add(lblEmail);

		contentPanel.add(emailTextField);
		separator.setBounds(10, 116, 474, 2);

		contentPanel.add(separator);
		lblUsername.setBounds(10, 129, 62, 14);

		contentPanel.add(lblUsername);

		contentPanel.add(usernameTextField);
		lblPassword.setBounds(10, 154, 60, 14);

		contentPanel.add(lblPassword);
		passwordField.setBounds(75, 151, 170, 20);

		contentPanel.add(passwordField);
		lblRepassword.setBounds(10, 179, 77, 14);

		contentPanel.add(lblRepassword);
		repasswordField.setBounds(90, 176, 155, 20);

		contentPanel.add(repasswordField);

		JLabel noteLabel = new JLabel(
				"Note : All fields have * it's important and cannt be null .");
		noteLabel.setBounds(10, 5, 265, 20);
		contentPanel.add(noteLabel);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						try {
							validateFields();
							
								
							} catch (DAOException e) {
								e.printStackTrace();
						
							}
					}
				});

				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {

						System.exit(0);
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

	private void validateFields() throws  DAOException {

		UsernamSrv userSrv = new UsernamSrv(DBUtil.getConnection());
		String miss = "You must provide : \n";
		if (firstNameTextField.getText().isEmpty()
				|| lastNameTextField.getText().isEmpty()
				|| usernameTextField.getText().isEmpty()
				|| passwordField.getText().isEmpty()
				|| repasswordField.getText().isEmpty()) {
			if (firstNameTextField.getText().isEmpty()) {
				miss += "First Name.\n";
			}

			if (lastNameTextField.getText().isEmpty()) {
				miss += "Last Name.\n";
			}

			if (usernameTextField.getText().isEmpty()) {
				miss += "Username.\n";
			}

			if (passwordField.getText().isEmpty()) {
				miss += "Password.\n";
			}

			if (repasswordField.getText().isEmpty()) {
				miss += "Repeated Password.\n";
			}
			JOptionPane.showMessageDialog(null, miss, null,
					JOptionPane.ERROR_MESSAGE);

			if (passwordField.getText().equals(repasswordField.getText()) == false) {
				JOptionPane.showMessageDialog(null,
						"Repeated password does not match password", null,
						JOptionPane.ERROR_MESSAGE);
			} 
				
		} else if (userSrv.usernameIsExsist(usernameTextField.getText())) {
			JOptionPane.showMessageDialog(null,
					"Username is exsist, Please Enter other ", null,
					JOptionPane.ERROR_MESSAGE);
		} else {
				
				try {
					UserInfo.getInstance().getService().getUsernamSrv()
							.register(getUsernameData(), getContactData());
				} catch (Exception e) {
					e.printStackTrace();
				}
					dispose();
			JOptionPane.showMessageDialog(null,
					"Registration is complete  successful",null,JOptionPane.INFORMATION_MESSAGE);
			new LoginDialog().setVisible(true);
		}

	}

	private Username getUsernameData() {
		Username username = new Username();
		username.setUsername(usernameTextField.getText());
		username.setPassword(passwordField.getText());
		username.setPasswordConfirm(repasswordField.getText());
		if (UserInfo.getInstance().getService().getUsernamSrv()
				.isAdminCreatedBefor()) {

			username.setRole("user");
		} else {
			username.setRole("admin");
		}
		return username;
	}

	private Contact getContactData() throws DAOException {
		// Username username = new Username();
		//
		// username.setUsername(usernameTextField.getText());
		// username.setPassword(passwordField.getText());
		// username.setPasswordConfirm(repasswordField.getText());
		// if (UserInfo.getInstance().getService().getUsernamSrv()
		// .isAdminCreatedBefor()) {
		//
		// username.setRole("user");
		// } else {
		// username.setRole("admin");
		// }
		// DAOFactory.getInstance().getUsernameDAO().insert(username);

		Contact contact = new Contact();

		contact.setFirstName(firstNameTextField.getText());
		contact.setSecondName(secondNameextField.getText());
		contact.setLastName(lastNameTextField.getText());
		contact.setMobileNumber(MobilPhoneTextField.getText());
		contact.setEmail(emailTextField.getText());
		// int usernameId = UserInfo.getInstance().getService().getUsernamSrv()
		// .getUsernamePK(usernameTextField.getText());
		// contact.setUsernameId(usernameId);

		// DAOFactory.getInstance().getContactDAO().insert(contact);
		return contact;

	}
}
